from src.conf.base_conf import BaseConf


class RobotRegex(BaseConf):
    def __init__(self, path: str):
        """机器人正则文件"""
        super().__init__(path)

    def _reload_from_dict(self, conf_dict: dict):
        self.content = conf_dict.get("正则匹配", [
            {
                "正则": "(?<=\\[DLS]\\[INFO]).+",
                "执行命令": "group %s",
                "过滤列表": []
            },
            {
                "正则": "(?<=There are).+(?=players online)",
                "执行命令": "group 当前有%s个玩家在线:",
                "输出追加行数": 1
            },
            {
                "正则": "Player connected: (.+), xuid: (.+)",
                "执行命令": "group %s1进入了服务器"
            },
            {
                "正则": "Player disconnected: (.+), xuid: (.+)",
                "执行命令": "group %s1退出了服务器"
            },
            {
                "正则": "\\[Chat] <(.+)> (.+)",
                "执行命令": "group %s1: %s2",
                "过滤列表": ["\\w+\\.[a-zA-Z]+", "[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+"]
            },
            {
                "正则": "(?<=Unknown command: ).+?(?=\\.)",
                "执行命令": "group 未知命令: %s"
            },
            {
                "正则": "(?<=Syntax error: ).+",
                "执行命令": "group 格式错误: %s"
            },
            {
                "正则": "(?<=Opped: ).+",
                "执行命令": "group 成功给予 %s 管理员权限"
            },
            {
                "正则": "(?<=De-opped: ).+",
                "执行命令": "group 成功取消 %s 管理员权限"
            },
            {
                "正则": "(?<=Could not op \\(already op or higher\\): ).+",
                "执行命令": "group 玩家 %s 已是管理员"
            },
            {
                "正则": "No targets matched selector",
                "执行命令": "group 没有匹配的对象"
            },
            {
                "正则": "Player added to allowlist",
                "执行命令": "group 成功添加白名单"
            },
            {
                "正则": "Player removed from allowlist",
                "执行命令": "group 成功移除白名单"
            },
            {
                "正则": "Player already in allowlist",
                "执行命令": "group 玩家已在白名单中"
            },
            {
                "正则": "Game rule (.+) has been updated to (.+)",
                "执行命令": "group 游戏规则 %s1 已更新为 %s2"
            },
            {
                "正则": "\\[DLSConfigEditor] dlsload",
                "执行命令": "dlsload"
            }
        ])

    def _check(self):
        if type(self.content) is not list:
            self.robot_re_list = []
            print("[DLS][WARN] 正则匹配格式异常！此功能将被禁用")

    def __str__(self) -> str:
        conf_dict = {"正则匹配": self.content}
        return self._dict_to_str(conf_dict)
